package com.slashvision.im.contact

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import com.slashvision.im.ui.UImage
import com.slashvision.im.ui.UText
import io.openim.android.sdk.models.ConversationInfo
import io.openim.android.sdk.models.UserInfo
import com.slashvision.im.R
import com.slashvision.im.tool.TimeUtil


/**
 * 联系人列表
 */
@ExperimentalComposeUiApi
@Composable
fun ContactList(contacts: List<UserInfo>, onItemClick: (UserInfo) -> Unit) {
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        items(contacts) { contact ->
            ContactItem(contact = contact, onClick = { onItemClick(contact) })
        }
    }
}

/**
 * 联系人项
 */
@ExperimentalComposeUiApi
@Composable
fun ContactItem(contact: UserInfo, onClick: () -> Unit) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp, vertical = 8.dp)
            .clickable { onClick() }
    ) {
        UImage(request = contact.icon, width = 40.dp, height = 40.dp)
        Spacer(modifier = Modifier.width(8.dp))
        UText(text = contact.name, size = 14.sp, color = colorResource(id = R.color.black_2))
    }
}


@ExperimentalComposeUiApi
@Composable
fun LastConvList(convs: List<ConversationInfo>, onItemClick: (ConversationInfo) -> Unit) {
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        items(convs) { conv ->
            ConvItem(conv = conv, onClick = { onItemClick(conv) })
        }
    }
}

@ExperimentalComposeUiApi
@Composable
fun ConvItem(conv: ConversationInfo, onClick: () -> Unit) {
    Row(modifier = Modifier
        .fillMaxWidth()
        .clickable { onClick() }) {
        with(conv) {
            // 头像
            UImage(request = faceUrl, width = 40.dp, height = 40.dp)
            Column(modifier = Modifier.weight(1f)) {
                // 姓名
                UText(text = showName, size = 14.sp, color = colorResource(id = R.color.black_2))
                Row() {
                    if (unreadCount > 1) {
                        // 未读消息数量
                        UText(
                            text = "[${unreadCount}条]",
                            size = 14.sp,
                            color = colorResource(id = R.color.black_4)
                        )
                    }
                    // 最后一条消息的内容
                    UText(
                        text = latestMsg,
                        size = 14.sp,
                        color = colorResource(id = R.color.black_4)
                    )
                }
            }
            // 发送时间
            Column() {
                UText(
                    text = TimeUtil.TSTR_3(latestMsgSendTime),
                    size = 14.sp,
                    color = colorResource(id = R.color.black_4)
                )
            }
        }
    }
}